using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;

namespace LenninSoftware.FormEditor.Adapters
{
    class Lookup : BaseControlAdapter
    {

        private SPWeb _web;

        public SPWeb Web
        {
            get { return _web; }
            set { _web = value; }
        }

        public Lookup(SPWeb web)
        {
            _web = web;

        }

        public override Control CreateReadOnlyControl(FieldInfo field)
        {
            Label label = new Label();
            return label;
        }

        public override void LoadReadOnlyData(Control control, SPListItem item, object data)
        {
            Label label = (Label)control;

            if (data == null)
            {
                label.Text = String.Empty;
                return;
            }

            string selectedValue = (string)data;

            if (selectedValue.Contains(";#"))
            {
                int index = selectedValue.IndexOf(";#") + 2;
                selectedValue = selectedValue.Substring(index);
            }

            label.Text = selectedValue;
        }

        public override System.Web.UI.Control CreateControl(FieldInfo field)
        {
            SPFieldInfo spField = (SPFieldInfo)field;

            SPFieldLookup lookup = (SPFieldLookup)spField.InternalField;
            DropDownList ddl1 = new DropDownList();

            ddl1.Items.Add(String.Empty);

            //lookup.LookupField 

            try
            {

                SPList list = null;
                Guid listID = Guid.Empty; 

                try
                {
                    listID = new Guid(lookup.LookupList);
                }
                catch (Exception)
                {

                }

                if (listID == Guid.Empty)
                    list = Web.Lists[lookup.LookupList];
                else
                    list = Web.Lists[listID];

                foreach (SPListItem item in list.Items)
                {
                    ddl1.Items.Add(new ListItem(item[lookup.LookupField].ToString(), item.ID.ToString()));
                }
            }
            catch (Exception)
            {

            }

            return ddl1;
        }

        public override void LoadData(System.Web.UI.Control control, SPListItem item, object data)
        {
            if (data == null)
                return;

            string selectedValue = (string)data;

            if (selectedValue.Contains(";"))
                selectedValue = selectedValue.Substring(0,selectedValue.IndexOf(';'));

            DropDownList ddl = (DropDownList)control;
            ddl.SelectedValue = selectedValue;
        }

        public override object SaveData(System.Web.UI.Control control, SPListItem item, object currentData)
        {
            DropDownList ddl = (DropDownList)control;
            return ddl.SelectedValue;
        }

        public override void Reset(System.Web.UI.Control control)
        {
            DropDownList ddl = (DropDownList)control;
            ddl.ClearSelection();
        }

        public override bool isEmpty(Control control)
        {
            DropDownList ddl = (DropDownList)control;
            return ddl.SelectedIndex == -1;
        }
    }
}
